|
|
|
|
|
|
|
specified criteria. In the WHERE clause, you can use any of the comparison predicates defined in the earlier section "Using the Comparison Predicate." Following is an example of the DELETE statement used to eliminate all customers who live in Florida: |
|
|
|
|
|
|
|
|
DELETE FROM Customers WHERE State='FL' |
|
|
|
|
CAUTION
|
|
|
|
|
After the DELETE statement is executed, the records are gone and can't be recovered. The only exception is if transaction processing is used. If you're using transaction processing, you can use a ROLLBACK statement to recover any deletions made since the last BEGINTRANS statement was issued. |
|
|
|
|
|
|
|
|
|
|
Using the INSERT Statement |
|
|
|
|
|
|
|
|
Like the DELETE statement, the INSERT statement is another action query. The INSERT statement is used in conjunction with the SELECT statement to add a group of records to a table. The syntax of the statement is as follows: |
|
|
|
|
|
|
|
|
INSERT INTO tablename SELECT rest-of-select-statement |
|
|
|
|
|
|
|
|
You build the SELECT portion of the statement exactly as explained in the first part of this chapter in the section "Using SELECT Statements.'' The purpose of the SELECT portion of the statement is to define the records to be added to the table. The INSERT statement defines the action of adding the records and specifies the table that is to receive the records. |
|
|
|
|
|
|
|
|
One use of the INSERT statement is to update tables created with the SELECT INTO statement. Suppose that you're keeping a church directory. When you first create the directory, you create a mailing list for the current member list. Each month, as new members are added, you either can rerun the SELECT INTO query and re-create the table, or you can run the INSERT INTO query and add only the new members to the existing mailing list. Listing C.18 shows the creation of the original mailing list and the use of the INSERT INTO query to update the list. |
|
|
|
|
|
|
|
|
Listing C.18 INSERT.TXTUsing the INSERT INTO Statement to Add a Group of Records to a Table |
|
|
|
|
|
|
|
|
'*******************************
'Create a new mailing list table
'*******************************
SELECT CS.Firstname & ' ' & CS.Lastname, CS.Address, ZP.City, _
ZP.State, CS.ZIP INTO Mailings FROM Members AS CS, _
Zipcode IN USZIPS AS ZP WHERE CS.Zip = ZP.Zip
'**********************************
'Update the mailing list each month
'**********************************
INSERT INTO Mailings SELECT CS.Firstname & ' ' & CS.Lastname, _
CS.Address, ZP.City, ZP.State, CS.ZIP _
FROM Customers AS CS, Zipcode IN USZIPS AS ZP _
WHERE CS.Zip = ZP.Zip AND CS.Memdate>Lastmonth |
|
|
|
|
|